package MP
{
	import flash.utils.ByteArray;
	
	/**
	 * ...
	 * @author 谭跃文
	 */
	public class MapdataTranfromExpand extends MapdataTranfrom
	{
		
		public function MapdataTranfromExpand()
		{
			super();
		
		}
		
		override public function tranfromToMapVO(bytes:ByteArray):MapVO
		{
			if (mapdata == null)
			{
				mapdata = new MapExpandVO();
			}
			var data:Object = JSON.parse(bytes.readMultiByte(bytes.length, "utf-8"));
			var mapVo:MapExpandVO = convertMapVO(data) as MapExpandVO;
			mapVo.hasBottomMap = data.hasBottomMap;
			if (mapVo.hasBottomMap)
			{
				mapVo.bottomMapWidth = data.bottomCountWidth;
				mapVo.bottomMapHeight = data.bottomCountHeight;
				mapVo.bottomTileArray = [];
				mapVo.clipsData = data.clipData;
				var tileCol:uint = Math.ceil(mapVo.bottomMapWidth / MapConfig.MAP_CLIP_IMAGE_WIDTH);
				var tileRow:uint = Math.ceil(mapVo.bottomMapHeight / MapConfig.MAP_CLIP_IMAGE_HEIGHT);
				mapVo.bottomMapCol = tileCol;
				mapVo.bottomMapRow = tileRow;
				var i:int, j:int, num:int;
				for (i = 0; i < tileRow; i++)
				{
					if (mapVo.bottomTileArray[i] == null)
					{
						mapVo.bottomTileArray[i] = [];
					}
					for (j = 0; j < tileCol; j++)
					{
						num = tileCol * i + j + 1;
						mapVo.bottomTileArray[i][j] = num;
					}
				}
			}
			else
			{
				mapVo.clipsData = null;
				mapVo.bottomMapWidth = 0;
				mapVo.bottomMapHeight = 0;
				mapVo.bottomMapCol = 0;
			}
			return mapVo;
		}
		
		private function convertMapVO(obj:Object):MapVO
		{
			var node:NodeExpand;
			var i:int;
			var k:int;
			var buffer:int;
			var obj2:Object;
			var num:int = 0;
			mapdata.dispose();
			var mapvo:MapVO = mapdata;
			mapvo.version = obj.version;
			mapvo.mapId = obj.id;
			mapvo.mapWidth = obj.width;
			mapvo.mapHeight = obj.height;
			mapvo.nodeWidth = obj.gridWidth;
			mapvo.nodeHeight = obj.gridHeight;
			mapvo.tileWidth = obj.tileWidth;
			mapvo.tileHeight = obj.tileHeight;
			mapvo.col = obj.col;
			mapvo.row = obj.row;
			mapvo.name = obj.name;
			var list:Array = obj.gridList;
			MapConfig.MAP_NODE_WIDTH = mapvo.nodeWidth;
			MapConfig.MAP_NODE_HEIGHT = mapvo.nodeHeight;
			var nodeArray:Array = [];
			for (var j:int = 0; j < mapvo.row; j++)
			{
				for (i = 0; i < mapvo.col; i++)
				{
					if (nodeArray[j] == null)
					{
						nodeArray[j] = [];
					}
					buffer = list[j * mapvo.col + i];
					node = NodeExpand.getNode();
					node.x = i;
					node.y = j;
					node.normalType = buffer;
					node.walkAble = Boolean(buffer & 1);
					node.ride = Boolean(buffer & 1 << 1);
					node.ispk = Boolean(buffer & 1 << 2);
					node.isSafe = Boolean(buffer & 1 << 3);
					node.isMask = Boolean(buffer & 1 << 4);
					node.isJump = Boolean(buffer & 1 << 5);
					node.isWabao = Boolean(buffer & 1 << 6);
					nodeArray[j][i] = node;
				}
			}
			mapvo.nodeArray = nodeArray;
			var special:Array = obj.special as Array;
			if (special)
			{
				for (k = 0; k < special.length; k++)
				{
					obj2 = special[k];
					node = mapvo.getNode(obj2.y, obj2.x) as NodeExpand;
					node.specialType = obj2.type;
					node.isTaFang = Boolean(obj2.type & 1);
					node.isXunLongJue = Boolean(obj2.type & 1 << 2);
					node.isWeiXian = Boolean(obj2.type & 1 << 3);
					if (node.isXunLongJue == false)
					{
						node.isXunLongJue = Boolean(obj2.type & 1 << 4);
					}
					if (node.isWeiXian)
					{
						mapvo.isHasWeixianArea = true;
					}
				}
			}
			var tileCol:uint = Math.ceil(mapvo.mapWidth / MapConfig.MAP_CLIP_IMAGE_WIDTH);
			var tileRow:uint = Math.ceil(mapvo.mapHeight / MapConfig.MAP_CLIP_IMAGE_HEIGHT);
			mapvo.tileArray = [];
			for (i = 0; i < tileRow; i++)
			{
				if (mapvo.tileArray[i] == null)
				{
					mapvo.tileArray[i] = [];
				}
				for (j = 0; j < tileCol; j++)
				{
					num = tileCol * i + j + 1;
					mapvo.tileArray[i][j] = num;
				}
			}
			return mapvo;
		}
	
	}

}